package com.example.hao.ximalaya.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.hao.ximalaya.R;
import com.example.hao.ximalaya.pojo.HotData;
import com.example.hao.ximalaya.pojo.Type20Or23ListItem;
import com.example.hao.ximalaya.utils.NumberUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Type20ViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.rv_item)
    RecyclerView rvItem;
    @BindView(R.id.rv_keyword)
    RecyclerView rv_keyword;
    private KeyWorldRVApdater keyWorldRVApdater;
    private HotBookRVAdapter hotBookRVAdapter;


    @OnClick(R.id.ll_change)
    public void change(View view) {
        Toast.makeText(context, "点击了换一批!", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.tv_more)
    public void more(View view) {
        Toast.makeText(context, "点击了更多!", Toast.LENGTH_SHORT).show();
    }

    public Type20ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {

        rv_keyword.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

        keyWorldRVApdater = new KeyWorldRVApdater(context);
        rv_keyword.setAdapter(keyWorldRVApdater);

        hotBookRVAdapter = new HotBookRVAdapter(context);
        rvItem.setAdapter(hotBookRVAdapter);
    }

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    List<HotData.ListBeanX.KeywordsBean> keywords;
    ArrayList<Type20Or23ListItem> listItems;

    public void setData(HotData.ListBeanX data) {
        tvTitle.setText(data.getTitle());
        CharSequence text = tvTitle.getText();
        if (text.equals("人文")) {
            rvItem.setLayoutManager(new GridLayoutManager(context, 3, LinearLayoutManager.HORIZONTAL, false));
        } else {
            rvItem.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        }

        //处理第一个RecyclerView
        keywords = data.getKeywords();
        keyWorldRVApdater.setKeywords(keywords);


        //处理第二个RecyclerView
        List<JsonObject> list = data.getList();
        listItems = new ArrayList<>();
        for (JsonObject obj : list) {
            Type20Or23ListItem listItem = new Gson().fromJson(obj, Type20Or23ListItem.class);
            listItems.add(listItem);
        }

        hotBookRVAdapter.setListItems(listItems);
    }


    class KeyWorldRVApdater extends RecyclerView.Adapter<KeyWorldRVApdater.KeyWorldViewHolder> {

        List<HotData.ListBeanX.KeywordsBean> keywords;
        Context context;

        public KeyWorldRVApdater(Context context) {
            this.context = context;
        }

        public void setKeywords(List<HotData.ListBeanX.KeywordsBean> keywords) {
            this.keywords = keywords;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public KeyWorldViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new KeyWorldViewHolder(LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_20_rv1_item, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull KeyWorldViewHolder holder, int position) {
            holder.tvKeyworld.setText(keywords.get(position).getKeywordName());
        }

        @Override
        public int getItemCount() {
            return keywords == null ? 0 : 4;
        }

        class KeyWorldViewHolder extends RecyclerView.ViewHolder {

            TextView tvKeyworld;

            public KeyWorldViewHolder(View itemView) {
                super(itemView);
                tvKeyworld = (TextView) itemView;
            }
        }
    }


    class HotBookRVAdapter extends RecyclerView.Adapter<HotBookRVAdapter.HotBookViewHolder> {

        ArrayList<Type20Or23ListItem> listItems;
        Context context;

        public HotBookRVAdapter(Context context) {
            this.context = context;
        }

        public void setListItems(ArrayList<Type20Or23ListItem> listItems) {
            this.listItems = listItems;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public HotBookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_20_rv2_item, parent, false);
            return new HotBookViewHolder(layout);
        }

        @Override
        public void onBindViewHolder(@NonNull HotBookViewHolder holder, int position) {
            Type20Or23ListItem item = listItems.get(position);

            Glide.with(context).load(item.getPic()).into(holder.ivIcon);
            holder.tvTitle.setText(item.getTitle());
            holder.tvSubtitle.setText(item.getSubtitle());
            holder.tvTrack.setText(String.valueOf(item.getTracksCount()));

            int playsCount = item.getPlaysCount();
            String s = NumberUtils.number2String(playsCount);
            holder.tvNum.setText(s);

        }

        @Override
        public int getItemCount() {
            return listItems == null ? 0 : listItems.size();
        }


        class HotBookViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.cv_icon)
            CardView cvIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;
            @BindView(R.id.tv_subtitle)
            TextView tvSubtitle;
            @BindView(R.id.tv_num)
            TextView tvNum;
            @BindView(R.id.tv_track)
            TextView tvTrack;
            @BindView(R.id.ll_bottom)
            LinearLayout llBottom;

            HotBookViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
